package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.MeetingUserSignature;
import com.ruoyi.system.mapper.MeetingUserSignatureMapper;
import com.ruoyi.system.service.IMeetingUserSignatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MeetingLeaveMapper;
import com.ruoyi.system.domain.MeetingLeave;
import com.ruoyi.system.service.IMeetingLeaveService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 会议请假审批Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-11
 */
@Service
public class MeetingLeaveServiceImpl implements IMeetingLeaveService
{
    @Autowired
    private MeetingLeaveMapper meetingLeaveMapper;
    @Autowired
    private IMeetingUserSignatureService meetingUserSignatureService;
    @Autowired
    private MeetingUserSignatureMapper meetingUserSignatureMapper;

    /**
     * 查询会议请假审批
     *
     * @param id 会议请假审批主键
     * @return 会议请假审批
     */
    @Override
    public MeetingLeave selectMeetingLeaveById(String id)
    {
        return meetingLeaveMapper.selectMeetingLeaveById(id);
    }

    /**
     * 查询会议请假审批列表
     *
     * @param meetingLeave 会议请假审批
     * @return 会议请假审批
     */
    @Override
    public List<MeetingLeave> selectMeetingLeaveList(MeetingLeave meetingLeave)
    {
        return meetingLeaveMapper.selectMeetingLeaveList(meetingLeave);
    }

    /**
     * 新增会议请假审批
     *
     * @param meetingLeave 会议请假审批
     * @return 结果
     */
    @Override
    public int insertMeetingLeave(MeetingLeave meetingLeave)
    {
        String date = DateUtils.dateTimeNow();
        String id = date + IdUtils.fastSimpleEnglishUUID(5);
        meetingLeave.setId(id);
        meetingLeave.setCreateTime(DateUtils.getNowDate());
        return meetingLeaveMapper.insertMeetingLeave(meetingLeave);
    }

    /**
     * 修改会议请假审批
     *
     * @param meetingLeave 会议请假审批
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMeetingLeave(MeetingLeave meetingLeave)
    {
        meetingLeave.setUpdateTime(DateUtils.getNowDate());
        int i = meetingLeaveMapper.updateMeetingLeave(meetingLeave);
        MeetingLeave leave = meetingLeaveMapper.selectMeetingLeaveById(meetingLeave.getId());
        MeetingUserSignature meetingUserSignature = new MeetingUserSignature();
        meetingUserSignature.setUserId(leave.getUserId());
        meetingUserSignature.setMeetingId(leave.getMeetingId());
        MeetingUserSignature signature = meetingUserSignatureMapper.selectMeetingUserSignature(meetingUserSignature);
        if(!ObjectUtils.isEmpty(leave)){
            String status = meetingLeave.getStatus();
            if("1".equals( status)){
                //审批通过，修改签到信息为已请假
                signature.setApprovalNum(leave.getId());
                signature.setStatus("2");
                meetingUserSignatureService.updateMeetingUserSignature(signature);
            }else if("2".equals( status)){
                //（通过之后）审批撤销，修改签到信息为未签到
                signature.setStatus("0");
                meetingUserSignatureService.updateMeetingUserSignature(signature);
            }
        }
        return i;
    }


    /**
     * 删除会议请假审批信息
     *
     * @param id 会议请假审批主键
     * @return 结果
     */
    @Override
    public int deleteMeetingLeaveById(String id)
    {
        return meetingLeaveMapper.deleteMeetingLeaveById(id);
    }
}
